﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Benefit.Models.View;

namespace Benefit.Service.View
{
    public class ServiceOperatorDayPosition : Benefit.Interface.View.IOperatorDayPosition
    {
        Benefit.DB.DBManager db = null;
        public ServiceOperatorDayPosition(Benefit.DB.DBManager db)
        {
            this.db = db;
        }

        /// <summary>
        /// 这个方法用的不对，应该从持仓明细里面去读，这样就有手数和成交价了
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        /// <param name="operatorId"></param>
        /// <returns></returns>
        public List<OperatorDayPosition> GetAllAccountDayPositionDelivery(int tradeHistoryId, int operatorId)
        {
            var query = db.OperatorDayPosition.Where(a => a.OperatorId == operatorId).Where(a => a.TradeHistoryId == tradeHistoryId).Where(a => a.Volume > 0);
            return query.ToList();
        }

        /// <summary>
        /// 初始化用户持仓信息
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        /// <param name="db"></param>
        public void InitOperatorDayPosition(int tradeHistoryId)
        {
            Interface.Sys.IOperator iop = new Sys.ServiceOperator(db);
            Interface.Sys.IDayOperatorAccount idoa = new Sys.ServiceDayOperatorAccount(db);
            Interface.Sys.ITradeHistory ith = new Sys.ServiceTradeHistory(db);
            Interface.Day.IAccountDayPositionDelivery adpd = new Day.ServiceAccountDayPositionDelivery(db);
            var operators = iop.GetUnDeletedOperators();
            foreach (Models.Sys.Operator op in operators)
            {
                List<Models.Sys.Account> accounts = idoa.GetDayOperatorAccounts(ith.GetDateTimeFromTradeHistoryId(tradeHistoryId), op.Id);
                foreach (Models.Sys.Account account in accounts)
                {
                    List<Models.Day.AccountDayPositionDelivery> list = adpd.GetAccountDayPositionDelivery(account.Id, tradeHistoryId);
                    foreach (Models.Day.AccountDayPositionDelivery s in list)
                    {
                        OperatorDayPosition change = new OperatorDayPosition();
                        change.OperatorId = op.Id;
                        change.Brokerid = s.Brokerid;
                        change.AccountId = s.AccountId;
                        change.CloseAmount = s.CloseAmount;
                        change.ExchangeId = s.ExchangeId;
                        change.Exchmargin = s.Exchmargin;
                        change.CloseProfitByDate = s.CloseProfitByDate;
                        change.CloseProfitByTrade = s.CloseProfitByTrade;
                        change.CloseVolume = s.CloseVolume;
                        change.CombinStrumentId = s.CombinStrumentId;
                        change.Commission = s.Commission;
                        change.Direction = s.Direction;
                        change.FloatingProfit = s.FloatingProfit;
                        change.HedgeFlag = s.HedgeFlag;
                        change.InstrumentId = s.InstrumentId;
                        change.Investorid = s.Investorid;
                        change.Margin = s.Margin;
                        change.MarginRateByMoney = s.MarginRateByMoney;
                        change.MarginRateByVolume = s.MarginRateByVolume;
                        change.OpenDate = s.OpenDate;
                        change.OpenPrice = s.OpenPrice;
                        change.OpenTime = s.OpenTime;
                        change.PositionProfit = s.PositionProfit;
                        change.PositionProfitByDate = s.PositionProfitByDate;
                        change.PositionProfitByTrade = s.PositionProfitByTrade;
                        change.ProductId = s.ProductId;
                        change.SettleMentPrice = s.SettleMentPrice;
                        change.TradeHistoryId = tradeHistoryId;
                        change.TradeId = s.TradeId;
                        change.TradeType = s.TradeType;
                        change.TradingDay = s.TradingDay;
                        change.Volume = s.Volume;
                        change.YesterdaySettlement = s.YesterdaySettlement;
                        db.OperatorDayPosition.Add(change);
                    }
                }
            }
            db.SaveChanges();
        }
        /// <summary>
        /// 删除某日用户持仓信息
        /// </summary>
        /// <param name="tradeHistory"></param>
        /// <param name="db"></param>
        public void Delete(int tradeHistory)
        {
            var query = db.OperatorDayPosition.Where(a => a.TradeHistoryId == tradeHistory);
            foreach (OperatorDayPosition p in query)
            {
                db.OperatorDayPosition.Remove(p);
            }
            db.SaveChanges();
        }
    }
}
